<?php

namespace app\common;



use Qcloud\Cos\Client;

/**
 * 腾讯云短信类
 */
class Txcos
{
    private $secretId = 'AKID0sa6PVN0NWQ5VgcJlyxhCSSCgurkQq33';
    private $secretKey = 'mNluoSr0uYw57wWryqVu4ze3EAqHSO4V';
    private $region = 'ap-beijing';
    private $cosname = 'khrs-1305850055';
    private $cos;


    public function __construct(){
        $this->cos = new Client([
            'region' => $this->region,
            'schema' => 'http', //协议头部，默认为http
            'credentials'=> [
                'secretId'  => $this->secretId ,
                'secretKey' => $this->secretKey
            ]
        ]);
    }


    /**
     * 上传文件
     * @param $key 文件名
     * @param $localPath 本地绝对路径
     */
    public function uploadFile($key,$localPath){
        try {
            $bucket = $this->cosname; //存储桶名称 格式：BucketName-APPID
            //此处的 key 为对象键，对象键是对象在存储桶中的唯一标识
            $result = $this->cos->upload($bucket,$key,fopen($localPath,'rb'));
            return true;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }



    /**
     * 获取文件下载路径
     */
    public function getDownUrl($key){
        try {
            $bucket = $this->cosname; //存储桶，格式：BucketName-APPID
            //此处的 key 为对象键，对象键是对象在存储桶中的唯一标识
            $signedUrl = $this->cos->getObjectUrl($bucket, $key, '2099-11-11');
            // 请求成功
            return $signedUrl;
        } catch (\Exception $e) {
            // 请求失败
            return false;
        }
    }
}